package com.ruoyi.app.utils;

import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

/**
 * @Author Alan Zhang
 * @Date 2025/4/1 16:39
 */
public class RandomUtil {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    private static SecureRandom secureRandom = new SecureRandom();

    public static String genPromotionCode(){
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        String randomPart = uuid.toString().substring(26, 36);
        System.out.println("=======genPromotionCode======="+ randomPart);
        return randomPart;
    }


    public static String genBuyerCode(){
        int length = 10; // 字符串的长度
        StringBuilder strSb = new StringBuilder(length);
        // 使用SecureRandom生成随机数
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        for (int i = 0; i < length; i++) {
            int index = secureRandom.nextInt(characters.length());
            strSb.append(characters.charAt(index));
        }

        System.out.println("=======genPromotionCode======="+strSb.toString());
        return strSb.toString();
    }


    public static String generateOrderNumber() {
        // 获取当前时间并格式化
        String timestamp = LocalDateTime.now().format(formatter);
        // 生成一个UUID
        UUID uuid = UUID.randomUUID();
        // 取UUID的后几位作为随机部分
        String randomPart = uuid.toString().substring(19, 23);
        // 拼接时间戳和随机部分
        return timestamp + randomPart;
    }

    public static String generatePaymentNumber() {
        // 生成一个UUID
        UUID uuid = UUID.randomUUID();
        // 取UUID的后几位作为随机部分
        String randomPart = uuid.toString().replace("-", "");
        // 拼接时间戳和随机部分
        return randomPart;
    }

    public static void main(String[] args) {
        String d = genBuyerCode();
        System.out.println(d);
    }
}
